<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="80px">
			<div class="app-space-between">
				<div class="app-space-between-left">

					<el-form-item label="办公区:" prop="office">
						<el-select v-model="queryParams.office" @change="handleQuery" placeholder="请选择" clearable>
							<el-option value="1" label="光华路办公区" />
							<el-option value="2" label="复兴路办公区" />
						</el-select>
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="序号" type="index" width="55" align="center" />
			<el-table-column label="办公区" align="center" width="240">
				<template slot-scope="scope">
					<div>
						<span v-if="scope.row.office == '1'">光华路办公区</span>
						<span v-if="scope.row.office == '2'">复兴路办公区</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="电话" align="center" prop="phone" />
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改岗位对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
			<div style="padding-right: 40px;">
				<el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="diLogLoading">
					<el-row>
						<el-col :span="24">
							<el-form-item label="办公区:" prop="office">
								<el-radio-group v-model="form.office">
									<el-radio label="1">光华路办公区</el-radio>
									<el-radio label="2">复兴路办公区</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row v-for="(item,index) in form.phone" :key="index">
						<el-col :span="24">
							<el-form-item label="电话:" :prop="'phone.'+index+'.phone'" :rules="rules.phone">
								<el-input v-model="item.phone" placeholder="请输入电话"
									style="width:240px;margin-right: 14px;" />
								<el-button v-if="index == 0" type="primary" icon="el-icon-plus" circle size="mini"
									@click="handleAddPhone"></el-button>
								<el-button v-if="index > 0" type="danger" icon="el-icon-minus" circle size="mini"
									@click="handleDetelePhone(index)"></el-button>
									
								<el-button v-if="index > 0" type="info" icon="el-icon-top" circle size="mini"
									@click="handleUpPhone(index)"></el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<div slot="footer" class="dialog-footer" v-if="!diLogLoading">
				<el-button type="primary" @click="submitForm" :loading="btnLoading">{{form.id?'保 存':'确 定'}}
				</el-button>
				<el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		carPhoneDelete,
		carPhoneDetails,
		carPhoneEdit,
		carPhoneList,
		carPhoneAdd
	} from "@/api/carRental/contact";
	export default {
		name: "CarRentalContact",
		data() {
			return {
				// 遮罩层
				loading: true,
				btnLoading: false,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				diLogLoading: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					office: "",
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					office: {
						required: true,
						message: "请选择办公区",
						trigger: "blur"
					},
					phone: {
						required: true,
						trigger: "blur",
						validator: (rule, value, callback) => {
							let reg1 = /^1[3456789]\d{9}$/; //手机号
							let reg2 = /^((0\d{2,3})-)?(\d{7,8})$/; //固定电话
							if (!value) return callback(new Error('请输入手机号'));
							if (reg1.test(value)) return callback();
							if (reg2.test(value)) return callback();
							callback(new Error('请输入正确的手机号'));
						}
					}
				}
			}
		},
		created() {
			this.getList();
		},
		methods: {
			handleDetelePhone(index) {
				this.form.phone = [...this.form.phone.slice(0, index), ...this.form.phone.slice(index + 1, this.form.phone
					.length)];
			},
			handleUpPhone(index){
				let nowData = this.form.phone.slice(index,index+1);
				let data = [...this.form.phone.slice(0, index), ...this.form.phone.slice(index + 1, this.form.phone
					.length)];
					this.form.phone = [...data.slice(0,index-1),...nowData,...data.slice(index-1,data.length)];
			},
			handleAddPhone() {
				this.form.phone.push({
					phone: ""
				});
			},
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				carPhoneList(this.queryParams).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
			},
			// 表单重置
			reset() {
				this.form = {
					phone: [{
						phone: ""
					}], //电话
					office: "", //办公区
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.title = "新增";
				this.open = true;
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				let id = row.id || this.ids
				if (Array.isArray(id)) id = id.toString()
				this.title = "编辑";
				this.open = true;
				this.diLogLoading = true
				carPhoneDetails(id).then(response => {
					for (let i in this.form) this.form[i] = response.data[i]
					this.form.id = id;
					if(this.form.phone){
						this.form.phone = this.form.phone.split(",").map(phone=>{
							return {phone}
						});
					}else{
						this.form.phone = [];
					}
					this.diLogLoading = false
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.btnLoading = true;
						let form = JSON.parse(JSON.stringify(this.form));
						form.phone = form.phone.map(item => item.phone).join(",");
						if (form.id != undefined) {
							carPhoneEdit(form).then(response => {
								this.$modal.msgSuccess("编辑成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						} else {
							carPhoneAdd(form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let ids = row.goodsId || this.ids;
				if (Array.isArray(ids)) ids = ids.toString()
				this.$modal.confirm('此操作将要删除所选的内容，是否要继续？').then(function() {
					return carPhoneDelete(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/post/export', {
					...this.queryParams
				}, `post_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
